<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item :label="$t('供货商编码')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="code">
              <a-input v-model="model.code" :placeholder="$t('请输入供货商编码')"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('供货商名称')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" :placeholder="$t('请输入供货商名称')"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('类型')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="type">
              <j-dict-select-tag
                type="list"
                v-model="model.type"
                dictCode="bis_goods_supplier.type"
                :placeholder="$t('请选择类型')"
                :disabled="disableType"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-if="model.type ==1">
            <a-form-model-item :label="$t('选择登录用户')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="userId">
              <a-select v-model="model.userId" :placeholder="$t('请选择用户')">
                <a-select-option :value="undefined">{{$t('请选择')}}</a-select-option>
                <a-select-option v-for="(item,i) in userList" :key="i" :value="item.value">{{item.text}}</a-select-option>
              </a-select>
              <a-button type="primary" @click="addUser">{{$t('新增')}}</a-button>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('图片')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="picture">
              <j-image-upload isMultiple v-model="model.picture"></j-image-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('联系人')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contacts">
              <a-input v-model="model.contacts" :placeholder="$t('请输入联系人')"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('联系电话')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="phone">
              <a-input v-model="model.phone" :placeholder="$t('请输入联系电话')"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('微信')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="wechat">
              <a-input v-model="model.wechat" :placeholder="$t('请输入微信')"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('qq')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="qq">
              <a-input v-model="model.qq" :placeholder="$t('请输入qq')"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('联系地址')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="address">
              <a-input v-model="model.address" :placeholder="$t('请输入联系地址')" :disabled="true"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('备注')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="comment">
              <a-input v-model="model.comment" :placeholder="$t('请输入备注')"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('开户银行')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="bank">
              <a-input v-model="model.bank" :placeholder="$t('请输入开户银行')"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('银行卡号')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="bankNo">
              <a-input v-model="model.bankNo" :placeholder="$t('请输入银行卡号')"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-item>
              <j-map @getMapValue="getMapValue" :city="defaultValue" :centerPoint="centerPoint"></j-map>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
    <user-modal ref="userModel" @ok="selectedUser"></user-modal>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'
import JMap from './JMap'
import UserModal from '@/views/system/modules/UserModal'
import { initDictOptions } from '@/components/dict/JDictSelectUtil'

export default {
  name: 'BisGoodsSupplierForm',
  components: {
    JMap,
    UserModal
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
  },
  data() {
    return {
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        code: [{ required: true, message: this.$t('请输入供货商编码') }],
        name: [{ required: true, message: this.$t('请输入供货商名称') }],
        type: [{ required: true, message: this.$t('请输入类型') }],
        userId: [{ required: true, message: this.$t('请输入登录用户') }],
        contacts: [{ required: false, message: this.$t('请输入联系人') }],
        phone: [{ required: false, message: this.$t('请输入联系电话') }],
      },
      url: {
        add: '/goods/bisGoodsSupplier/add',
        edit: '/goods/bisGoodsSupplier/edit',
        queryById: '/goods/bisGoodsSupplier/queryById',
      },
      defaultValue: [],
      centerPoint: '',
      userList:[],
      disableType: false
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    add(record) {
      this.modelDefault.type = record.type
      this.edit(this.modelDefault)
    },
    edit(record) {
      initDictOptions('sys_user,username,id').then(res=>{
        if(res.success){
          this.userList=res.result
        }
      })
      this.model = Object.assign({}, record)
      this.visible = true
    },
    getMapValue(obj) {
      this.$set(this.model,'address',obj.address)
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
    addUser() {
      this.$refs.userModel.add()
    },
    selectedUser(name){
      let that=this
      initDictOptions('sys_user,username,id').then(res=>{
        if(res.success){
          that.userList=res.result
          let obj=res.result.filter(rs=>{return rs.text==name})[0];
          console.log(obj)
          that.$nextTick(function(){
            that.model.userId=obj.value
          })
        }
      })
    },
  },
}
</script>